package com.example.h3x.androidmalware.View.Dialogs;

import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by h3x on 28.02.18.
 * Simple dialog showing log info
 */

public class LogDialog  extends DialogFragment {
    private String out;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        try {
            //convert logs to string
            Process process = Runtime.getRuntime().exec("logcat -d");
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
            StringBuilder log=new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                log.append(line);
            }
            out = log.toString();
        } catch (IOException e) {
            out = "Error with getting logs:" + e.getMessage();
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Log");
        builder.setMessage(out);
        builder.setPositiveButton("CLOSE", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            }
        });
        builder.setNegativeButton("COPY", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                try{
                    ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
                    ClipData clip = ClipData.newPlainText("message", out);
                    clipboard.setPrimaryClip(clip);
                    Toast toast = Toast.makeText(getContext(),"Copied to buffer", Toast.LENGTH_SHORT);
                    toast.show();
                }
                catch (NullPointerException e){
                    Toast toast = Toast.makeText(getContext(),"Got Some problems :c", Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        });
        return builder.create();
    }




}
